package com.example.supernote.ui.note.components

import android.net.Uri
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView

@Composable
fun ShowMedia(
    sourceUri: Uri
) {
    val context = LocalContext.current
    val player = ExoPlayer.Builder(context).build().apply {
        val mediaItem = MediaItem.fromUri(sourceUri)
        setMediaItem(mediaItem)
        playWhenReady = false
    }.also { player ->
        player.prepare()
    }

    AndroidView(
        modifier = Modifier
            .width(350.dp)
            .height(250.dp),
        factory = {
            PlayerView(it).also { playerView ->
                playerView.player = player
            }
        },
        onRelease = { playerView ->
            playerView.removeAllViews()
            playerView.player!!.release()
        }
    )
}